<%@ page contentType="text/html;charset=utf-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
	<head>
		<title>楼宇创建</title>
		<link rel="stylesheet"
			href="<%=request.getContextPath()%>/css/common.css" type="text/css" />
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/dwr/interface/buildingsMgrAction.js'></script>
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/dwr/engine.js'></script>
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/dwr/util.js'></script>
		<script type="text/javascript"
			src="<%=request.getContextPath()%>/js/N13FormValidation-2.js"></script>
		<script type="text/javascript">
		var apartmentfloornum =-1//全局变量
		var roomnum=0;
		var apartmentname;

		
	function createApart() {
	 apartmentname =$("apartmentname").value;
	 apartmentfloornum = $("apartmentfloornum").value;
	var apartmentmgrname = $("apartmentmgrname").value;
	var apartmenttelephone =$("apartmenttelephone").value;
	if(apartmentname==""){
		alert("请填写公寓名!");
		return;
		}
	if(apartmentfloornum==""){
		alert("请填写公寓楼层数!");
		return;
		}
    //公寓电话合法性判断，现在为空
	/*if(apartmenttelephone!=""){
		if(!testIsphone(apartmenttelephone)){
		alert("输入的公寓电话号码不合法!");
		return ;	
		}
	}*/
	if(!testIsNum(apartmentfloornum)){
		alert("输入的公寓楼层数有误,应该为数字！");
		return ;
		}
	buildingsMgrAction.insertApartment(apartmentname,apartmentfloornum,apartmentmgrname,apartmenttelephone,insertapartcallback)
	}
	var insertapartcallback = function insert_cb(status){
		alert(status);
		//显示楼层的select框
		if("该公寓已存在!"==status){
			DWRUtil.removeAllOptions( $("floorName"));  
				return false;
			}else{
				if($('page_nav').style.visibility == 'visible'){//进入向导，将导航页隐藏
					$('page_nav').style.visibility = 'hidden';
				}
				if($('dorm_template').style.display == "none"){
					$('dorm_template').style.display = "";
				}
				if($('apt_creation').style.visibility == 'visible'){
					$('apt_creation').style.visibility = 'hidden';
				}
					var sf = $("floorName");
					DWRUtil.removeAllOptions(sf);  
					sf.options[0] = new Option("请选择楼层",'0');
					//alert(aprt_floorno);
					for ( var j = 1; j < (parseInt(apartmentfloornum)+1); j++) {
						var optt = new Option(j+"层",j);
						sf.options[j] = optt;
					}
			}		
		}
		
	function testIsNum(strInput){
	var myReg = /^[1-9]\d*$/;///^[1-9]+[0-9]*$/i 
	if(myReg.test(strInput)) return true;
  	return false
	}

	function testIsphone(strInput){
	var myReg = /^((0[0-9]{2,3}\-)?([2-9][0-9]{6,7})+(\-[0-9]{1,4})?)?$/i; 
	if(myReg.test(strInput)) return true;
  	return false
	}
	
	//建立公寓的模板页面，可以自行修改
	function createDormTemplate(){
		if($("floorName").selectedIndex == 0){
			alert("请选择楼层");
			return;
		}
		
		/**
		* 清空上次的table
		*/
		var stable = $("floorTable");
		DWRUtil.removeAllRows(stable);
		roomnum=0;
		
		var showfl = $("floorName").options[$("floorName").selectedIndex].value;
		var fn;
		var lodgegenderdisp;
		var dormitoryfuncdisp;
		var lodgegender =$("lodgegender").options[$("lodgegender").selectedIndex].value;
		//alert(lodgegender);
		if(lodgegender==1){
			 lodgegenderdisp ="男";
		 }else {
			 lodgegenderdisp ="女";
			 }
		if($('afloornum').value==""){
			alert("请输入每层房间数！");
			return false;
			}
		if(!testIsNum($('afloornum').value)){
		alert("输入的每层房间数有误,应该为数字！");
		return ;
		}	
		var floorroomtype = $("floorroomtype").value;
		if(floorroomtype==""){
			alert("请输入房型！");
			return false;
			}
			
		if(!testIsNum(floorroomtype)){
		alert("输入的房型有误,应该只为数字！");
		return ;
		}
		var roomfee = $("roomfee").value;
		if(roomfee==""){
			alert("请输入宿舍费用！");
			return false;
			}
		if(!testIsNum(roomfee)){
		alert("输入的房间费用有误,应该为数字！");
		return ;
		}	
	   		
		var dormitoryfunc = $("dormitoryfunc").options[$("dormitoryfunc").selectedIndex].value;
		if(dormitoryfunc==1){
			 dormitoryfuncdisp ="住宿";
		 }else {
			 dormitoryfuncdisp ="仓库";
			 }
		//开始创建模版了.页面显示
		if($('orgin_template').style.visibility == 'hidden'){
			$('orgin_template').style.visibility = 'visible';
		}
		for(var a = 0;a<$('afloornum').value;a++){
			if(roomnum<9){
				fn = '0'+(roomnum+1);
				}else{
					fn = roomnum+1;
					}
			myrow=$("floorTable").insertRow(roomnum);	
			myrow.id= "tr"+roomnum;
			c=myrow.insertCell(0);
			c=myrow.insertCell(1);
			c.innerHTML="<input type='text' name='afloornum"+roomnum+"' id='afloornum"+roomnum+"' value='"+showfl+fn+"' size=9 />";
			c=myrow.insertCell(2);
			c.innerHTML="<select style='width:80' name='floorroomtype"+roomnum+"' id='floorroomtype"+roomnum+"'><option value='6'>6人间</option><option value='4'>4人间</option><option value='2'>2人间</option><option value='3'>3人间</option><option value='5'>5人间</option></select>";
			confireSelectTag($('floorroomtype'),$('floorroomtype'+roomnum));

			c=myrow.insertCell(3);
			c.innerHTML="<select name='lodgegender"+roomnum+"' id='lodgegender"+roomnum+"'><option value='1'>男</option><option value='0'>女</option></select>";
			confireSelectTag($('lodgegender'),$('lodgegender'+roomnum));
			c=myrow.insertCell(4);
			c.innerHTML="<input type='text' name='roomfee"+roomnum+"' id='roomfee"+roomnum+"' value='"+roomfee+"' size=9  />";
			c=myrow.insertCell(5);
			c.innerHTML="<input type='hidden' name='apartmentname"+roomnum+"' id='apartmentname"+roomnum+"' value='"+apartmentname+"'size=9   />";
			c=myrow.insertCell(6);
			c.innerHTML="<input type='hidden' name='floorno"+roomnum+"' id='floorno"+roomnum+"' value='"+showfl+"' size=9  />";
			c=myrow.insertCell(7);
			c.innerHTML="<input type='text' name='dormdisplayname"+roomnum+"' id='dormdisplayname"+roomnum+"' value='"+apartmentname+showfl+"层"+showfl+fn+"房间"+"' size=19 />";
			c=myrow.insertCell(8);
			
			c.innerHTML="<select name='dormitoryfunc"+roomnum+"' id='dormitoryfunc"+roomnum+"'><option value='1'>住宿</option><option value='0'>仓库</option>	</select>";
			confireSelectTag($('dormitoryfunc'),$('dormitoryfunc'+roomnum));
			//c.innerHTML="<input type='text' name='dormitoryfunc"+roomnum+"' id='dormitoryfunc"+roomnum+"' value='"+dormitoryfuncdisp+"' size=9   />";
			roomnum++;
			
								
		}
		//alert($("afloornum1").value);
	}
	//判断两个不同select标签值，让obj2的选项和obj1的被选项相同
	function confireSelectTag(obj1,obj2){
		for(var a = 0;a<obj2.options.length;a++){
			if(obj2.options[a].value == obj1.options[obj1.selectedIndex].value){
				obj2.options[a].selected = "selected";
			}
		}	
	}

	//公寓模板页面的更新提交
	function submitAfloorDorm(){
		var jsonArr = new Array();
		var dormnamearr = new Array(); 
		var tbl = $("floorTable");
		//alert( tbl.rows.length+"aa")
		//var jsonobj[2]_2;
		//var jsonobj[7]_7;
		for(var i=0; i < tbl.rows.length; i++) {
		var jsonobj =  new Array(); 
		var nid =  "afloornum"+i;
		var rtid = "floorroomtype"+i;
		var gdid = "lodgegender"+i;
		var rmid = "roomfee"+i;
		var aptnm = "apartmentname"+i;
		var flno = "floorno"+i;
		var dmdisplayname = "dormdisplayname"+i;
		var dmfc = "dormitoryfunc"+i;
		
		jsonobj[0] = $(nid).value+""; //dormitoryname
		jsonobj[1] = $(rtid).options[$(rtid).selectedIndex].value+""; // dormitoryroomtype
		jsonobj[2]=  $(gdid).options[$(gdid).selectedIndex].value+""; //dormitorysex
		jsonobj[3] = $(rmid).value+""; //dormitoryfee
		jsonobj[4] = $(aptnm).value+""; //apartmentname
		jsonobj[5] = $(flno).value+"";  //dormitoryfloorno
		jsonobj[6] = $(dmdisplayname).value+"";  //dormdisplayname
		jsonobj[7] = $(dmfc).options[$(dmfc).selectedIndex].value+""; //dormitoryfunc
		//alert($(nid).value); 
		//alert($(rtid).value);
		//alert($(gdid).value);
		//alert($(rmid).value);
		var jo =jsonobj.join("_");
		jsonArr.push(jo); 
		dormnamearr.push(jsonobj[0]);
		}
		alert(jsonArr);
		//alert(dormnamearr); //101,102,103,104,105,106,107,110
		//判断公寓名是否有重复
		var dnl = dormnamearr.length;
		for(var j=0;j<dnl;j++){
			for(var k=j+1;k<=dnl+j;k++){
				if(dormnamearr[j]==dormnamearr[k]){
					alert('宿舍名'+dormnamearr[j]+'有重复，请重新输入！');
					return ;
				}
			}
		}
		buildingsMgrAction.updateBatchDormitoryInfo(jsonArr+"",updatebatchcb)
		}
	var updatebatchcb = function updatebatch_cb(status){
		if($('orgin_template').style.visibility == 'visible'){
			$('orgin_template').style.visibility = 'hidden';
		}
		alert(status);
		}
	
</script>
	</head>
	<body>
		<div id="page_container">
			<%@include file="mgrbanner.jsp"%>
			<h2>
				创建公寓楼
			</h2>
			<!-- 公寓创建的div -->
			<div id="apt_creation" align="center" style="visibility: visible">
				<table class="table_common">
					<tr>
						<td class="td_left">
							公寓名:
						</td>
						<td class="td_right">
							<input type="text" id="apartmentname" />
						</td>
					</tr>
					<tr>
						<td class="td_left">
							公寓楼层数:
						</td>
						<td class="td_right">
							<input type="text" id="apartmentfloornum" />
						</td>
					</tr>
					<tr>
						<td class="td_left">
							公寓管理员姓名:
						</td>
						<td class="td_right">
							<input type="text" id="apartmentmgrname" />
						</td>
					</tr>
					<tr>
						<td class="td_left">
							公寓电话:
						</td>
						<td class="td_right">
							<input type="text" id="apartmenttelephone" />
						</td>
					</tr>
				</table>
				<input class="common_button" type="submit" value="创建公寓"
					onclick="javascript:createApart();" />
			</div>

			<!-- 显示楼层和每层修改的div -->
			<div id="dorm_template" style="display:none">
			<table class="table_common">
					<tr>
						<td class="td_left">
							选择楼层：
						</td>
						<td class="td_right">
							<select class="common_select" name="floorName" id="floorName"
								style="width: 100">
							</select>
						</td>
					</tr>
					<tr>
						<td class="td_left">
							每层房间数:
						</td>
						<td class="td_right">
							<input type="text" name="afloornum" id="afloornum" size='10' />
						</td>
					</tr>
					<tr>
						<td class="td_left">
							每层房型:
						</td>
						<td class="td_right">
						<select class="common_select" name="floorroomtype" id="floorroomtype"
								style="width: 80">
								<option value="2">
									2人间
								</option>
								<option value="3">
									3人间
								</option>
								<option value="4">
									4人间
								</option>
								<option value="5">
									5人间
								</option>
								<option value="6">
									6人间
								</option>
							</select>
						</td>
					</tr>
					<tr>
						<td class="td_left">
							住宿性别:
						</td>
						<td class="td_right">
							<select class="common_select" name="lodgegender" id="lodgegender"
								style="width: 80">
								<option value="1">
									男
								</option>
								<option value="0">
									女
								</option>
							</select>
						</td>
					</tr>
					<tr>
						<td class="td_left">
							房间作用:
						</td>
						<td class="td_right">
							<select class="common_select" name="dormitoryfunc"
								id="dormitoryfunc" style="width: 80">
								<option value="1">
									住宿
								</option>
								<option value="0">
									仓库
								</option>
							</select>
						</td>
					</tr>
					<tr>
						<td class="td_left">
							房间费用:
						</td>
						<td class="td_right">
							<input type="text" name="roomfee" id="roomfee" size='10' />
						</td>
					</tr>
				</table>
				<table>
					<tr>
						<td>
							<input class="long_button" type="submit" value="生成宿舍模版"
								onclick="javascript:createDormTemplate();" />
						</td>
					</tr>
				</table>
				</div>
			<!-- 生成宿舍的原始模板 -->
			<div id="orgin_template" style="visibility: hidden;">
				<table id="floorTable" class="table_common">
				</table>
				<input class="common_button" type="submit" value="提交更新"
					onclick="javascript:submitAfloorDorm();">
			</div>
		</div>
	</body>
</html>